﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Business;
using Entities;
namespace ServicesDirectory.UserControl
{
    public partial class ManagerSupportingMaterial : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var supportingMaterial = Request.QueryString["id"];
            if (!IsPostBack)
            {

                if (Request.UrlReferrer != null) btnBack.PostBackUrl = Request.UrlReferrer.ToString();
                if (supportingMaterial != null)
                    BindData(supportingMaterial);
            }


            btnSave.Click += BtnSave_Click;
           
        }

        

        void BtnSave_Click(object sender, EventArgs e)
        {
            var osupportingMaterial = new SupportingMaterial();
            osupportingMaterial.SupportingMaterialId = new Guid(btnSave.CommandName);
            osupportingMaterial.Type = ddlType.SelectedValue;
            osupportingMaterial.Url = txtUrl.Text;
            osupportingMaterial.Description = txtDescription.Text;
            osupportingMaterial.IsActive = true;
            lblMessage.Text = SupportMaterialBusiness.Instance.Update(osupportingMaterial);
            lblMessage.Visible = true;
        }
        void BindData(string supportingMaterialId)
        {
            var osupportingMaterial = SupportMaterialBusiness.Instance.GetSupportMaterial(supportingMaterialId);
            if (osupportingMaterial != null)
            {
                btnSave.CommandName = osupportingMaterial.SupportingMaterialId.ToString();
                txtDescription.Text = osupportingMaterial.Description;
                txtUrl.Text = osupportingMaterial.Url;
                ddlType.SelectedValue = osupportingMaterial.Type;
                var oBusinessUser = new UserBusiness();
                var oUser = oBusinessUser.GetUserById(osupportingMaterial.UserId.ToString());
                if (oUser != null)
                {
                    lblAdddedBy.Text = oUser.Account;
                }
                lblAddedDate.Text = osupportingMaterial.AddedDate.ToString("dd-MM-yyyy");
            }
        }
    }
}